<template>
  <div style="width: 100%; height: 100%;">
    <div class="login-container">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">WeDonek快速开发平台</h3>
        </div>
        <el-form-item prop="LoginName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="loginName"
            v-model="loginForm.LoginName"
            placeholder="登陆名"
            name="loginName"
            type="text"
            tabindex="1"
            autocomplete="on"
            @change="checkLoginName"
          />
        </el-form-item>
        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="LoginPwd">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input
              :key="passwordType"
              ref="loginPwd"
              v-model="loginForm.LoginPwd"
              :type="passwordType"
              placeholder="登陆密码"
              name="loginPwd"
              tabindex="2"
              autocomplete="on"
              @keyup.native="checkCapslock"
              @blur="capsTooltip = false"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>
        <el-form-item v-if="isVer" class="verCodeRow" prop="VerCode">
          <div class="row">
            <div class="input">
              <el-input v-model="loginForm.VerCode" :clearable="true" placeholder="验证码" />
            </div>
            <div class="verCode">
              <img style="width: 130px;" :src="verSrc" @load="codeLoad" @click="refresVerCode">
            </div>
          </div>
        </el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登陆</el-button>
      </el-form>
    </div>
    <userPwdChange :login-name="loginForm.LoginName" title="密码已过期请修改" :visible.sync="visible" />
  </div>
</template>

<script>
import * as loginApi from '@/api/login/empLogin'
import userPwdChange from './components/userPwdChange.vue'
export default {
  name: 'Login',
  components: {
    userPwdChange
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('登陆密码不能小于6位！'))
      } else {
        callback()
      }
    }
    return {
      isVer: false,
      verSrc: null,
      visible: false,
      loginForm: {
        LoginName: null,
        LoginPwd: null,
        VerCode: null
      },
      loginRules: {
        LoginName: [{ required: true, trigger: 'blur', message: '登陆名不能为空!' }],
        LoginPwd: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      isLoadCode: false
    }
  },
  mounted() {
    if (this.loginForm.LoginName === '') {
      this.$refs.loginName.focus()
    } else if (this.loginForm.LoginPwd === '') {
      this.$refs.loginPwd.focus()
    }
  },
  methods: {
    refresVerCode() {
      if (this.isLoadCode === false) {
        this.isLoadCode = true
        this.verSrc = loginApi.GetVerCodeSrc(this.loginForm.LoginName) + '&t=' + new Date().getTime()
      }
    },
    codeLoad() {
      this.isLoadCode = false
    },
    async checkLoginName() {
      if (this.loginForm.LoginName.length < 6) {
        return
      }
      const res = await loginApi.CheckIsVerCode(this.loginForm.LoginName)
      if (res) {
        this.verSrc = loginApi.GetVerCodeSrc(this.loginForm.LoginName)
        this.isVer = true
      } else {
        this.isVer = false
        this.verSrc = null
      }
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.loginPwd.focus()
      })
    },
    handleLogin() {
      const that = this
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', {
            LoginName: this.loginForm.LoginName,
            Password: this.loginForm.LoginPwd,
            VerCode: this.loginForm.VerCode
          })
            .then(() => {
              this.jumpIndex()
            })
            .catch((e) => {
              that.loading = false
              if (e.param === 'verCode:true') {
                that.openVer(that.loginForm.LoginName)
              } else if (e.errorcode === '205022731408005') {
                this.isVer = false
                this.visible = true
              } else if (e.errorcode === '204955529462405') {
                this.$notify({
                  title: '警告',
                  message: '用户已锁定请' + e.param + '后重试!',
                  type: 'warning'
                })
              } else if (e.errorcode === '204955418423941') {
                that.refresVerCode()
              }
            })
        } else {
          return false
        }
      })
    },
    openVer(name) {
      this.isLoadCode = false
      if (this.isVer) {
        this.refresVerCode()
      } else {
        this.verSrc = loginApi.GetVerCodeSrc(name)
        this.isVer = true
      }
    },
    jumpIndex() {
      const query = this.$route.query
      this.$router.push({ name: 'loading', query: query })
      this.loading = false
    }
  }
}
</script>

<style lang="scss">

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .verCodeRow .el-form-item__content {
    width: 100%;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
  .verCodeRow {
    height: 50px;
    line-height: 50px !important;
    display: inline-flex;
    width: 100%;
  }
  .verCodeRow .row {
    display: inline-flex;
    width: 100%;
  }
  .verCodeRow .input {
    width: 70%;
    display: inline-flex;
  }
  .verCodeRow .input .el-input{
    width: 100%;
  }
  .verCodeRow .verCode {
    width: 130px;
    float: right;
    display: inline-flex;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
